package de.hawhamburg.shc.livingplace;



import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration;
import android.util.Log;

import java.util.List;

//import de.hawhamburg.shc.controller.Settings;

public class WlanManager {
	
	
	private  WifiManager wifiManager;
	//private Settings settings;
	
	public WlanManager(Activity activity) 
		{
		//this.settings = settings;
		wifiManager = (WifiManager)activity.getSystemService(Context.WIFI_SERVICE);
	   
		}
	
	 public boolean connectTo(String networkId) {
	        if (wifiManager.isWifiEnabled()) {
	            List<WifiConfiguration> configuredWlans = wifiManager.getConfiguredNetworks();
	            for(WifiConfiguration networkSSID : configuredWlans) {
	                if (networkSSID.SSID != null && networkSSID.SSID.equals("\"" + networkId + "\"")) {
	                    Log.i("connectTo", networkSSID.SSID);
	                    wifiManager.disconnect();
	                    wifiManager.enableNetwork(networkSSID.networkId, true);
	                    wifiManager.reconnect();

	                    try {
	                        Thread.sleep(7000);
	                        return true;
	                    }
	                    catch(final InterruptedException e) {
	                        e.printStackTrace();
	                    }
	                }
	            }
	            return false;
	        }
	        else {
	            return false;
	        }
	    }
	
	
}
